<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card
        v-loading="loading"
        class="tree-card"
      >
        <!-- 用了一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDeptment(company)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="FullDatabase"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 用了一个行列布局 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown @command="onItemClick">
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item :command="{type:'add',data:data}">添加子部门</el-dropdown-item>
                        <el-dropdown-item :command="{type:'edit',data:data}">编辑部门</el-dropdown-item>
                        <el-dropdown-item :command="{type:'del',id:data.id}">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <addDept ref="addDept" :show-dialog="showDialog" :industry-title="industryTitle" :all-statistics="allStatistics" @closeDoalog="closeDoalog" @reloadList="getDepartments" />
    </div>
  </div>
</template>

<script>
import addDept from './components/add-dept.vue'

import { getDepartmentsApi, delDepartmentsApi } from '@/api/departments'
import { TranslistTreeData } from '@/utils'

export default {
  name: 'Departments',
  components: {
    addDept
  },
  data() {
    return {
      // 转换前的全部数据
      depts: [],
      // 转换后的全部数据
      FullDatabase: [],
      // 选择器显示内容匹配名字
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      // 头部数据
      company: {},
      // 卡片显示隐藏
      showDialog: false,
      // 当前点击的下级部门数据
      industryTitle: {},
      // 所有的部门数据
      allStatistics: [],
      loading: false
    }
  },
  // 钩子函数
  created() {
    this.getDepartments()
  },
  // 获取组织架构数据
  methods: {
    async  getDepartments() {
      this.loading = true
      const { data: res } = await getDepartmentsApi()
      this.allStatistics = res.depts
      // console.log(res)
      this.depts = TranslistTreeData(res.depts, '')
      // 全部数据
      this.FullDatabase = this.depts
      // 公司名字
      this.company.name = res.companyName
      // 一级架构的id
      this.company.id = ''
      // 将所有组件当做子级传进来
      this.company.children = res.depts
      // console.log(this.FullDatabase)
      this.loading = false
    },
    // 判断点击的是哪个
    onItemClick({ type, id, data }) {
      if (type === 'add') {
        // console.log('添加操作')
        this.industryTitle = data
        this.newlyIncreased()
      }
      if (type === 'edit') {
        console.log('编辑操作')
        this.editDepts(data)
      }
      if (type === 'del') {
        // console.log('删除操作')
        this.delDepartments(id)
      }
    },
    // 编辑部门
    editDepts(data) {
      this.showDialog = true
      this.industryTitle = data
      // console.log(this.industryTitle)
      this.$nextTick(() => {
        this.$refs.addDept.ModifyTheDepartment()
      })
    },
    // 删除部门
    delDepartments(id) {
      this.$confirm('是否删除该部门？', '提示').then(() => {
        delDepartmentsApi(id)
        this.$message.success('删除成功')
        this.getDepartments()
      }).catch(() => {})
    },
    // 新增部门
    newlyIncreased() {
      this.showDialog = true
    },
    // 子组件传递来的事件
    closeDoalog() {
      this.showDialog = false
    },
    // 点击头部添加时，封装好的数据传入，再通过子组件校验名字的方法校验
    addDeptment(data) {
      // 打开卡片
      this.showDialog = true

      // 将所有数据传给子组件方法的参数里
      this.industryTitle = data

      // // 通过ref方法获取到子组件的事件
      // this.$refs.addDept.CustomCheckname()
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
